<?php
// +-------------+-------------+------+-----+---------+----------------+
// | Field       | Type        | Null | Key | Default | Extra          |
// +-------------+-------------+------+-----+---------+----------------+
// | id          | int(11)     | NO   | PRI | NULL    | auto_increment |
// | name        | varchar(60) | NO   |     |         |                |
// | value       | text        | NO   |     | NULL    |                |
// | autoload    | tinyint(1)  | NO   |     | 1       |                |
// | member_id   | int(11)     | NO   |     | 0       |                |
// | create_time | int(11)     | NO   |     | 0       |                |
// | update_time | int(11)     | NO   |     | 0       |                |
// +-------------+-------------+------+-----+---------+----------------+
namespace app\common\model;

use think\Model;
use think\facade\Cache;
use think\facade\Config;
class Options extends Model
{
    /**
   * 保存Option设置
   * @Author zhanghong
   * @Date   2017-05-16
   * @param  string     $name [description]
   * @param  array      $value [description]
   * @return array
   */
  public function saveSetting($name, $values){
    $res = ['status' => false, 'msg' => '更新设置失败'];
    if(empty($name)){
      $res['msg'] = '更新设置名不能为空';
      return $res;
    }else if(!is_array($values)){
      $res['msg'] = '更新数据不正确';
      return $res;
    }

    $data = [
      'name' => $name,
      'value' => json_encode($values),
    ];

    $opt = $this->where('name', $name)->find();
    if(empty($opt)){
      $is_update = false;
    }else{
      $is_update = true;
      $data['id'] = $opt['id'];
    }
    $this->data($data)->isUpdate($is_update)->allowField(true)->save();

    // 删除旧缓存
    $cache_key = $this->itemCacheKey($name);
    Cache::rm($cache_key);

    $res = ['status' => true, 'msg' => '更新成功'];
    return $res;
  }

  /**
   * 读取Option值
   * @Author zhanghong
   * @Date   2017-05-16
   * @param  string     $name option name
   * @return array
   */
  public function readSetting($name){
    $cache_key = $this->itemCacheKey($name);
    $val = Cache::get($cache_key);
    if(!empty($val)){
      return $val;
    }

    $map = ["name" => $name];
    $default_val = [];

    if(empty($name)){
      return $default_val;
    }

    $opt = $this->where($map)->find();

    if(empty($opt)){
      return $default_val;
    }

    $val = json_decode($opt["value"], true);
    // 缓存过期时间在config.php文件里设置
    Cache::set($cache_key, $val);
    return $val;
  }

  /**
   * 读取全站所有设置数据
   * @Author zhanghong
   * @Date   2017-05-16
   * @return [type]     [description]
   */
  public function readAllConfig(){
    $list = [];
    $options = $this->field(["id", "name"])->select();
    foreach ($options as $key => $opt) {
      $opt_name = $opt["name"];
      $val = $this->readSetting($opt_name);
      $list[$opt_name] = $val;
    }
    return $list;
  }

  /**
   * 读取站点Host
   * @Author zhanghong
   * @Date   2017-05-16
   * @return [type]     [description]
   */
  public function getSiteHost(){
    $options = $this->readSetting("site_options");
    if(!empty($options) && isset($options["site_host"])){
      $site_root = $options["site_host"];
    }else{
      $site_root = Config::get("site_host");
    }
    return $site_root;
  }

  private function itemCacheKey($name)
  {
    return "option_{$name}";
  }
}
